package cn.itcast.controller;

import cn.itcast.constant.MessageConstant;
import cn.itcast.constant.RedisMessageConstant;
import cn.itcast.entity.Result;
import cn.itcast.pojo.Order;
import cn.itcast.pojo.Setmeal;
import cn.itcast.service.OrderService;
import cn.itcast.service.SetmealService;
import cn.itcast.utils.SMSUtils;
import com.alibaba.dubbo.config.annotation.Reference;
import com.aliyuncs.exceptions.ClientException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.JedisPool;

import java.util.Map;

/*
 *Author:TH
 *Dete:2019/10/2711:47
 */
@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private JedisPool jedisPool;
    @Reference
    private OrderService orderService;
    @Reference
    private SetmealService setmealService;

    @RequestMapping("/submit")
    public Result submit(@RequestBody Map map){
        //获取预约手机号
        String telephone = (String) map.get("telephone");
        //获取验证码
        String validateCode = (String) map.get("validateCode");
        //获取预约时间
        String orderDate = (String) map.get("orderDate");
        //redis中存储的验证码
        String validateRedis = jedisPool.getResource().get(telephone + RedisMessageConstant.SENDTYPE_ORDER);

        //判断用户输入的验证码是否匹配
        if ((validateRedis!=null) && (validateCode!=null) && (validateCode.equals(validateRedis))){
            //验证码匹配,发送预约

            Result result = null;
            try {
                //存入预约类型
                map.put("orderType", Order.ORDERTYPE_WEIXIN);
                result = orderService.order(map);
            } catch (Exception e) {
                e.printStackTrace();
                //预约失败
                return result;
            }

            //判断预约是否成功
            if(result.isFlag()){
                //预约成功,发送预约成功短信
                try {
                    //预约信息
                    String setmealId = (String) map.get("setmealId");
                    Setmeal setmeal = setmealService.findBysId(Integer.parseInt(setmealId));
                    SMSUtils.sendShortMessage(SMSUtils.ORDER_NOTICE,telephone,setmeal.getName(),orderDate);
                } catch (ClientException e) {
                    e.printStackTrace();
                }
            }
            return result;
        }else {
            //验证码不匹配
            return new Result(false, MessageConstant.VALIDATECODE_ERROR);
        }
    }


    //根据id查询预约信息
    @RequestMapping("/findById")
    public Result findById(Integer id){
        try {
            //查询成功
            Map map = orderService.findById(id);
           /* //获取手机号
            String telephone = (String) map.get("telephone");
            //获取预约时间
            String orderDate = (String) map.get("orderDate");
            //预约信息
            String setmeal = (String) map.get("setmeal");
            //为用户发送预约成功信息
            SMSUtils.sendShortMessage(SMSUtils.ORDER_NOTICE,telephone,setmeal,orderDate);*/

            return new Result(true,MessageConstant.QUERY_ORDER_SUCCESS,map);
        } catch (Exception e) {
            e.printStackTrace();
            //查询失败
            return new Result(false,MessageConstant.QUERY_ORDER_FAIL);
        }

    }
}
